package com.zr.d_c.serviceimpl;

import java.util.List;

import com.zr.d_c.dao.BuyCartDao;
import com.zr.d_c.dao.DishDao;
import com.zr.d_c.dao.SellerDao;
import com.zr.d_c.daoimpl.BuyCartDaoImpl;
import com.zr.d_c.daoimpl.DishDaoImpl;
import com.zr.d_c.daoimpl.SellerDaoImpl;
import com.zr.d_c.model.BuyCart;
import com.zr.d_c.model.Dish;
import com.zr.d_c.model.Seller;
import com.zr.d_c.service.BuyCartService;

import net.sf.json.JSONObject;

public class BuyCartServiceImpl implements BuyCartService {

	BuyCartDao buycartdao = new BuyCartDaoImpl();
	DishDao dishdao = new DishDaoImpl();
	SellerDao sellerdao = new SellerDaoImpl();
	@Override
	public JSONObject getBuyCartByCid(int cid, int start, int pageSize) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		json.put("total", buycartdao.selectCountDishOfBuyCartByCid(cid));
		List<BuyCart> buycarts = buycartdao.selectBuyCart(cid, start, pageSize);
		for (BuyCart buyCart : buycarts) {
			int did = buyCart.getDid();
			Dish d = dishdao.selectDishByDid(did);
			String dname = d.getDname();
			int dprice = d.getDprice();
			String dtemp1 = d.getDtemp1();
			int sid = d.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String sname = s.getSname();
			buyCart.setSid(sid);
			buyCart.setDname(dname);
			buyCart.setDprice(dprice);
			buyCart.setDtemp1(dtemp1);
			buyCart.setSname(sname);
		}
		json.put("rows", buycarts);
		return json;
	}
	@Override
	public int deleteBuyCartByBid(int[] bids) {
		// TODO Auto-generated method stub
		int flag = 0;
		for (int i = 0; i < bids.length; i++) {
			if(buycartdao.deleteBuyCartByBid(bids[i])>0){
				flag = 1;
			}else{
				flag = 0;
				break;
			}
		}
		return flag;
	}
	@Override
	public int modifyBuyCartByBid(int bnum, int bid) {
		// TODO Auto-generated method stub
		return buycartdao.updateBuyCartByBid(bnum, bid);
	}
	@Override
	public JSONObject getBuyCartByCidAndSname(int cid, int start, int pageSize, String sname) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		int total = buycartdao.selectCountDishOfBuyCartByCid(cid);
		List<BuyCart> buycarts = buycartdao.selectBuyCart(cid, start, pageSize);
		List<BuyCart> buycartss = buycartdao.selectBuyCartAndSname(cid, start, pageSize, sname);
		for (BuyCart buyCart : buycarts) {
			int did = buyCart.getDid();
			Dish d = dishdao.selectDishByDid(did);
			int sid = d.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String ssname = s.getSname();
			if(!ssname.equals(sname)){
				total = total - 1;
			}
		}
		for (BuyCart buyCart : buycartss) {
			int did = buyCart.getDid();
			Dish d = dishdao.selectDishByDid(did);
			String dname = d.getDname();
			int dprice = d.getDprice();
			String dtemp1 = d.getDtemp1();
			int sid = d.getSid();
			Seller s = sellerdao.selectSellerBySid(sid);
			String ssname = s.getSname();
			buyCart.setSid(sid);
			buyCart.setDname(dname);
			buyCart.setDprice(dprice);
			buyCart.setDtemp1(dtemp1);
			buyCart.setSname(ssname);
		}
		json.put("total", total);
		json.put("rows", buycartss);
		return json;
	}

}
